<cfparam name="t" default="">
<cfset view = "certificate">

<cfif Len(Trim(t)) lt 30 or Len(Trim(t)) gt 40>

	<p>Invalid token</p>
	<cfabort>
	<cfset view = "invalidToken" />
	
<cfelse>

	<cftry>

		<!--- lookup receipent --->
		<cfscript>
			storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
			qChampion = storedProcedureGateway.getChampion(Token = t);
		</cfscript>
		
		<cfif qChampion.RecordCount eq 1>
	
			<cfset certificateExists = FileExists("C:\inetpub\wwwroot\calgrants\applications\champions\" & t & ".pdf") />
			
			<cfif NOT certificateExists>

				<!--- set values for embedding --->
				<cfif qChampion.Champion_Type eq "student" or qChampion.Champion_Type eq "dream act">
					<cfset firstName = Trim(qChampion.First_Name)>
					<cfset lastName = Trim(qChampion.Last_Name)>
					<cfset highSchool = Trim(qChampion.High_School)>
				</cfif>
				
				<cfif qChampion.Champion_Type eq "student">
					<cfpdfform action="populate" source="CSAC_CalGrantChamp_StudentTemplate_Form_Color.pdf" name="populatedForm">
						<cfpdfformparam name="First_Name" value="#UCase(firstName)#">
						<cfpdfformparam name="Last_Name" value="#UCase(lastName)#">
						<cfpdfformparam name="High_School" value="#UCase(highSchool)#">
					</cfpdfform>
				<cfelseif qChampion.Champion_Type eq "dream act">
					<cfpdfform action="populate" source="CSAC_CalGrantChamp_DreamActTemplate_Form_Color.pdf" name="populatedForm">
						<cfpdfformparam name="First_Name" value="#UCase(firstName)#">
						<cfpdfformparam name="Last_Name" value="#UCase(lastName)#">
						<cfpdfformparam name="High_School" value="#UCase(highSchool)#">
					</cfpdfform>
				<cfelseif qChampion.Champion_Type eq "school">
					<cfpdfform action="populate" source="CSAC_CalGrantChamp_SchoolTemplate_Form_Color.pdf" name="populatedForm">
						<cfpdfformparam name="School" value="#UCase(school)#">
					</cfpdfform>
				<cfelseif qChampion.Champion_Type eq "district">
					<cfpdfform action="populate" source="CSAC_CalGrantChamp_DistrictTemplate_Form_Color.pdf" name="populatedForm">
						<cfpdfformparam name="District" value="#UCase(district)#">
					</cfpdfform>
				</cfif>
				
				<cfpdf action="write" flatten="yes" source="populatedForm" destination="#t#.pdf" overwrite="yes">

			</cfif>
			
		<cfelse>
		
			<p>Invalid user</p>
			<cfabort>
			<cfset view = "invalidUser" />
		
		</cfif>
	
		<cfcatch type="any">
			<!--- bury the error --->
			<cfdump var="#cfcatch#">
			<cfabort>
			<cfset view = "error" />
		</cfcatch>
	
	</cftry>

</cfif>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Cal Grant Champions</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="description" content="Cash for College Scholars">
	<meta name="author" content="Syntric">
	
	<cfoutput>
		<script src="#request.pathToJQuery#" type="text/javascript"></script>
		<script src="#request.pathToBootstrap#" type="text/javascript"></script>
		<script src="#request.pathToKendo#" type="text/javascript"></script>
		<script src="#request.pathToUI#/common.js" type="text/javascript"></script>
		<link href="#request.pathToBootstrapCSS#" rel="stylesheet" />
		<link href="#request.pathToKendoCommonCSS#" rel="stylesheet" />
		<link href="#request.pathToKendoDefaultCSS#" rel="stylesheet" />
		<link href="#request.pathToUI#/common.css" rel="stylesheet" />
		<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet" type="text/css" />
	</cfoutput>
	
	<script type="text/javascript">
		var sharer = "https://www.facebook.com/sharer/sharer.php?u=";
		var shareSimple = function(u) {
			window.open(sharer + u, 'sharer', 'width=626,height=436');
		}
		var share = function(cType) {
			var appID, url, title, description, redirect;
			if (cType == 'calgrant') {
				var appID = "132182413646963";
				name = encodeURIComponent("Cal Grant Champion");
				description = encodeURIComponent("I'm part of the Class of 2013 Cal Grant Champions - ask me what's next!");
				redirectURL = encodeURIComponent("http://www.csac.ca.gov");
				imageURL = encodeURIComponent("http://www.calgrants.org/common/images/Cal-Grant-Champion-Logo-FB.png");
				url = "https://www.facebook.com/dialog/feed?app_id=" + appID + "&link=" + redirectURL + "&picture=" + imageURL + "&name=" + name + "&caption=&description=" + description + "&redirect_uri=" + redirectURL;
			} else if (cType == 'dreamact') {
				appID = "514126808652760";
				name = encodeURIComponent("Dreams In Action");
				description = encodeURIComponent("I'm part of the first class of California Dream Act applicants - ask me what's next!");
				redirectURL = encodeURIComponent("http://www.caldreamact.org");
				imageURL = encodeURIComponent("http://www.calgrants.org/common/images/Dreams-In-Action-Logo-FB.png");
				url = "https://www.facebook.com/dialog/feed?app_id=" + appID + "&link=" + redirectURL + "&picture=" + imageURL + "&name=" + name + "&caption=&description=" + description + "&redirect_uri=" + redirectURL;
			}
			window.open(url, 'facebookDialog', 'width=1000,height=700');
		}
		var tweet = function(cType) {
			var url, description;
			if (cType == 'calgrant') {
				refURL = encodeURIComponent("http://www.csac.ca.gov");
				description = encodeURIComponent("I'm part of the Class of 2013 Cal Grant Champions - ask me what's next!");
				hashtags = encodeURIComponent("CalGrantChampion");
				via = encodeURIComponent("CAstudentaid");
				url = "https://twitter.com/share?via=" + via + "&url=" + refURL + "&text=" + description + "&hashtags=" + hashtags;
			} else if (cType == 'dreamact') {
				refURL = encodeURIComponent("http://www.caldreamact.org");
				description = encodeURIComponent("I'm part of the first class of California Dream Act applicants - ask me what's next!");
				hashtags = encodeURIComponent("DreamsInAction");
				via = encodeURIComponent("CAstudentaid");
				url = "https://twitter.com/share?via=" + via + "&url=" + refURL + "&text=" + description + "&hashtags=" + hashtags;
			}
			window.open(url, 'twitterDialog', 'width=1000,height=700');
		}
	</script>
</head>

<body>
<cfoutput>
<br><br><br>
<center>
<div class="container">
	<div class="row">
		<div class="span12"></div>
			<div class="hero-unit">
				<cfswitch expression="#view#">
					<cfcase value="certificate">
						<cfif qChampion.Champion_Type eq "student">
							<h1>HELLO, #qChampion.First_Name# #qChampion.Last_Name#!</h1>
							<p>We want to acknowledge you and help you share the good news.</p>
							<p><a href="#t#.pdf" target="_blank" class="btn btn-large btn-primary">Click to View or Print Certificate</a></p>
							<p><a href="##" onClick="share('calgrant'); return false;"><img src="http://www.calgrants.org/common/images/Facebook_64x64.png" border="0"></a> <a href="##" onClick="tweet('calgrant'); return false;"><img src="http://www.calgrants.org/common/images/Twitter_64x64.png" border="0"></a></p>
						<cfelseif qChampion.Champion_Type eq "dream act">
							<h1>HELLO, #qChampion.First_Name# #qChampion.Last_Name#!</h1>
							<p>We want to acknowledge you and help you share your accomplishment.</p>
							<p><a href="#t#.pdf" target="_blank" class="btn btn-large btn-primary">Click to View or Print Certificate</a></p>
							<p><a href="##" onClick="share('dreamact'); return false;"><img src="http://www.calgrants.org/common/images/Facebook_64x64.png" border="0"></a> <a href="##" onClick="tweet('dreamact'); return false;"><img src="http://www.calgrants.org/common/images/Twitter_64x64.png" border="0"></a></p>
						<cfelseif qChampion.Champion_Type eq "school">
				
						<cfelseif qChampion.Champion_Type eq "district">
				
						</cfif>
						
					</cfcase>
					<cfdefaultcase>
						<p>Certificate not found</p>
					</cfdefaultcase>
				</cfswitch>
			</div>

	</div>
</div>
</center>	
</cfoutput>
</body>
</html>